<?php
/**
 * @title 621. 任务调度器 - 简化
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String[] $tasks
     * @param Integer $n
     * @return Integer
     */
    function leastInterval($tasks, $n) {
        /**
         * @since 2020-12-08 计数
         */
        $chars = [];
        foreach ($tasks as $c){
            if(isset($chars[$c])){
                $chars[$c] += 1;
            } else {
                $chars[$c] = 1;
            }
        }

        /**
         * @since 2020-12-08 最大数
         */
        $max = max($chars);

        /**
         * @since 2020-12-08 空位时间
         */
        $blankT = ($max-1)*($n+1);

        /**
         * @since 2020-12-08 执行时间
         */
        $t = $blankT;

        /**
         * @since 2020-12-08 迭代
         */
        foreach ($chars as $count){
            /**
             * @since 2020-12-08 是否最多数量
             */
            if($count == $max){
                /**
                 * @since 2020-12-08 空位是否足够
                 */
                if($blankT >= $count-1){
                    $blankT -= $count-1;
                    $t++;
                } else {
                    /**
                     * @since 2020-12-08 空位不够
                     */
                    $t += $count-$blankT;
                    $blankT = 0;
                }
            } else {
                /**
                 * @since 2020-12-08 空位是否足够
                 */
                /**
                 * @since 2020-12-08 空位是否足够
                 */
                if($blankT >= $count){
                    $blankT -= $count;
                } else {
                    /**
                     * @since 2020-12-08 空位不够
                     */
                    $t += $count-$blankT;
                    $blankT = 0;
                }
            }
        }

        /**
         * @return
         */
        return $t;
    }
}



/**
 * @url http://127.0.0.1/leetcode/202009/2020.09.11_3.php
 */
$datas = [
    [["A","A","A","B","B","B"], 2], // 8
    [["A","A","A"], 2],
    // [["A","A","A","A","B","B","B"], 2],
    // [["A","A","A","A","B","B","B","C","C"], 2],
    // [["A","A","A","A","B","B","B","C","C","D","D"], 2],

    [["T","Y","V","F","Y","C","K","L","G","P","P","D","Z","E","A","C","C","Y","U","M","J","S","T","H","Y","G","T","G","I","L","O","C","K","K","H","I","N","S","U","U","H","J","X","G","O","X","I","R","W","C","D","F","U","W","M","T","C","G","Z","L","R","N","O","B","Y","W","K","L","O","E","F","V","O","D","C","C","A","L","T","X","N","W","C","I","S","O","B","V","U","A","H","M","O","V","O","M","S","Y","Y","G","D","E","C","S","H","F","U","I","Q","O","F","D","K","H","M","D","W","N","Z","Q","O","G","C","D","C","Q","Q","U","P","O","B","T","S","E","L","A","J","G","I","Z","U","N","D","F","V","P","I","R","D","I","H","R","O","K","V","R","B","L","L","R","Z","N","K","S","R","V","S","A","B","A","Z","W","O","D","B","J","S","K","A","W","S","I","O","H","S","J","Y","U","U","K","L","T","X","V","M","O","R","E","O","T","F","O","P","T","R","Q","D","K","B","D","G","T","M","U","A","E","D","Y","Z","Y","I","K","R","F","F","E","U","X","I","I","Q","O","X","G","H","O","X","K","Z","Y","O","F","R","A","A","R","F","E","Q","E","C","Z","O","U","F","U","Y","Z","R","H","H","I","V","F","O","C","U","L","N","T","K","C","Z","B","C","Z","T","H","E","J","M","G","J","A","A","O","V","Y","N","M","F","V","U","B","A","J","D","U","V","R","O","F","T","N","G","W","N","A","E","R","K","Q","X","T","R","Y","H","M","X","U","Y","D","Q","T","E","Q","D","I","L","Y","Z","Z","D","T","N","K","P","A","K","T","R","U","J","P","N","B","N","V","N","K","P","M","O","F","F","S","W","I","A","H","H","A","H","L","T","U","V","J","U","G","C","M","A","M","B","O","N","P","K","A","A","Z","M","O","F","S","G","C","B","H","K","I","H","S","T","B","M","P","K","H","V","M","T","W","Z","V","K","N","K","U","O","K","U","A","Z","A","T","F","D","U","N","O","D","U","G","X","V","S","M","G","A","H","T","U","D","S","P","O","G","A","J","U","L","E","U","K","F","O","Q","I","J","D","X","M","Y","D","J","U","W","V","B","W","D","U","Q","H","N","G","W","T","H","F","N","S","K","I","D","P","W","U","Y","F","Y","V","S","X","Y","B","R","U","X","S","R","B","N","H","J","A","O","F"], 3],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->leastInterval(...$data);
    // $result = $obj->find(...$data);
    // var_dump($result);
    // var_dump(count($result));
    if(count($result)<=20){
        var_dump($result);
    } else {
        var_dump(count($result));
    }
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". $data[1][$i][0] ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
